package repository

import (
	"context"
	"time"

	"github.com/google/uuid"
	"techoiceness.com/aiagent/llm-gateway/internal/entity"
)

// UserStats 用户统计信息
type UserStats struct {
	UserID            uuid.UUID `json:"user_id"`
	ConversationCount int64     `json:"conversation_count"`
	MessageCount      int64     `json:"message_count"`
	TotalTokensUsed   int64     `json:"total_tokens_used"`
	LastActivity      time.Time `json:"last_activity"`
	CreatedAt         time.Time `json:"created_at"`
}

// MessageFilter 消息过滤器
type MessageFilter struct {
	ConversationID string
	Role           string
	StartTime      *time.Time
	EndTime        *time.Time
	ContentSearch  string
	PageSize       int
	Page           int
}

// ClientMessageRepository 客户端消息仓库接口
type ClientMessageRepository interface {
	// 创建客户端消息
	CreateClientMessage(ctx context.Context, clientMessage *entity.ClientMessage) error

	// 更新客户端消息状态和回复
	UpdateClientMessage(ctx context.Context, clientMessage *entity.ClientMessage) error

	// 根据ID获取客户端消息
	GetClientMessageByID(ctx context.Context, id uuid.UUID) (*entity.ClientMessage, error)

	// 获取对话的客户端消息历史
	GetClientMessageHistory(ctx context.Context, conversationID uuid.UUID, limit, offset int) ([]entity.ClientMessage, error)

	// 获取用户的消息历史
	GetClientMessagesByUserID(ctx context.Context, userID uuid.UUID, limit, offset int) ([]entity.ClientMessage, error)

	// 删除客户端消息
	DeleteClientMessage(ctx context.Context, id uuid.UUID) error

	// 获取对话中客户端消息总数
	GetClientMessageCount(ctx context.Context, conversationID uuid.UUID) (int64, error)

	// 清理旧的客户端消息
	CleanupOldClientMessages(ctx context.Context, before time.Time) (int64, error)

	// 消息对相关方法
	// 根据消息对ID获取关联的消息（user和assistant）
	GetMessagesByPairID(ctx context.Context, pairID uuid.UUID) ([]entity.ClientMessage, error)

	// 获取用户消息对应的助手消息
	GetAssistantMessageByPairID(ctx context.Context, pairID uuid.UUID) (*entity.ClientMessage, error)

	// 获取助手消息对应的用户消息
	GetUserMessageByPairID(ctx context.Context, pairID uuid.UUID) (*entity.ClientMessage, error)
}

// UserRepository 用户仓库接口
type UserRepository interface {
	// 用户认证相关
	GetUserByUsername(ctx context.Context, username string) (*entity.User, error)
	GetUserByID(ctx context.Context, id uuid.UUID) (*entity.User, error)

	// 用户管理
	CreateUser(ctx context.Context, user *entity.User) error
	UpdateUser(ctx context.Context, user *entity.User) error
	DeleteUser(ctx context.Context, id uuid.UUID) error

	// 用户查询
	GetAllUsers(ctx context.Context, limit, offset int) ([]entity.User, int64, error)
	GetUsersByStatus(ctx context.Context, isActive bool, limit, offset int) ([]entity.User, int64, error)
	SearchUsers(ctx context.Context, keyword string, limit, offset int) ([]entity.User, int64, error)

	// 用户统计
	GetUserCount(ctx context.Context) (int64, error)
	GetUserStats(ctx context.Context, userID uuid.UUID) (*UserStats, error)
}

// AdminRepository 管理员仓库接口
type AdminRepository interface {
	// 管理员认证相关
	GetAdminByUsername(ctx context.Context, username string) (*entity.Admin, error)
	GetAdminByID(ctx context.Context, id uuid.UUID) (*entity.Admin, error)

	// 管理员管理
	CreateAdmin(ctx context.Context, admin *entity.Admin) error
	UpdateAdmin(ctx context.Context, admin *entity.Admin) error
	DeleteAdmin(ctx context.Context, id uuid.UUID) error

	// 管理员查询
	GetAllAdmins(ctx context.Context, limit, offset int) ([]entity.Admin, int64, error)
	GetAdminsByRole(ctx context.Context, role entity.AdminRole, limit, offset int) ([]entity.Admin, int64, error)

	// 登录记录
	UpdateLastLogin(ctx context.Context, adminID uuid.UUID) error

	// 管理员统计
	GetAdminCount(ctx context.Context) (int64, error)
}

// ConversationRepository 对话仓库接口 (客户端面向的对话实体)
type ConversationRepository interface {
	// 对话管理
	CreateConversation(ctx context.Context, conversation *entity.Conversation) error
	GetConversationByID(ctx context.Context, id uuid.UUID) (*entity.Conversation, error)
	UpdateConversation(ctx context.Context, conversation *entity.Conversation) error
	DeleteConversation(ctx context.Context, id uuid.UUID) error

	// 用户对话查询
	GetConversationsByUserID(ctx context.Context, userID uuid.UUID, limit, offset int) ([]entity.Conversation, int64, error)

	// 访问权限验证
	CheckConversationAccess(ctx context.Context, conversationID, userID uuid.UUID) (bool, error)

	// 对话统计和摘要
	GetConversationCountByUser(ctx context.Context, userID uuid.UUID) (int64, error)
	GetTotalConversationCount(ctx context.Context) (int64, error)
	GetConversationSummary(ctx context.Context, conversationID uuid.UUID) (*ConversationSummary, error)

	// 清理操作
	CleanupOldConversations(ctx context.Context, before time.Time) (int64, error)
	CleanupEmptyConversations(ctx context.Context) (int64, error)
}

// ConversationSummary 对话摘要
type ConversationSummary struct {
	ConversationID uuid.UUID `json:"conversation_id"`
	MessageCount   int64     `json:"message_count"`
	FirstMessage   time.Time `json:"first_message"`
	LastMessage    time.Time `json:"last_message"`
	TotalTokens    int64     `json:"total_tokens,omitempty"`
}
